package com.utils.fileLoader 
{
	import com.utils.loadManager.LoaderManager;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.net.FileReferenceList;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;
	
	public class WindowBulkSelectLoad extends Sprite
	{
		public static const instance:WindowBulkSelectLoad = new WindowBulkSelectLoad();
		private var _fileReferenceList:FileReferenceList;
		private var _file:FileReference;
		private var _loadIndex:int;
		private var _totalFileCount:int;
		private var _fileType:Array = [];
		private var _loaderList:Array = [];
		
		private var _loadOver:Function = new Function();
		public function WindowBulkSelectLoad()
		{
			if(instance) throw Error('不能重复实例化!');
		}
		
		public function openWondow():void
		{
			bulkFileLoad();
		}
		
		/**批量加载*/		
		private function bulkFileLoad():void
		{
			if(!_fileReferenceList) _fileReferenceList = new FileReferenceList();
			if(!_fileReferenceList.hasEventListener(Event.SELECT)){
				_fileReferenceList.addEventListener(Event.SELECT,onSelectBulkFileHandler);
			}
			if(!_fileReferenceList.hasEventListener(IOErrorEvent.IO_ERROR)){
				_fileReferenceList.addEventListener(IOErrorEvent.IO_ERROR,onIOErrorEventHandler);
			}
			_fileReferenceList.browse(getFileType());
		}
		
		private function getFileType():Array
		{
			_fileType.push(new FileFilter("图片", "*.jpg;*.jpeg;*.png"));
			_fileType.push(new FileFilter("SWF", "*.swf"));
			return _fileType;
		}
		
		private function onSelectBulkFileHandler(e:Event):void
		{
			trace("选择文件数量:"+_fileReferenceList.fileList.length);
			_totalFileCount = _fileReferenceList.fileList.length;
			loadFileLoop();
		}		
		
		private function loadFileLoop():void{
			for (var i:int = 0; i < _totalFileCount; i++) 
			{
				_file = _fileReferenceList.fileList[i];
				_file.addEventListener(IOErrorEvent.IO_ERROR,onIOErrorEventHandler);
				_file.addEventListener(Event.COMPLETE,onFileCompleteHandler);
				_file.load();
			}
		}
		
		private function onIOErrorEventHandler(e:IOErrorEvent):void
		{
			trace(e.text);
		}
		
		private function onFileCompleteHandler(e:Event):void
		{
			trace('文件名：' + _file.name);
			_file = FileReference(e.target);
			LoaderManager.instance.loadSource([_file.data],1);  
		}	
		
		public function set loadOver(value:Function):void{_loadOver = value;}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	}
}